﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace MolyMade.Http.DataTypes
{
    public class Headers
    {
        public string this[string key]
        {
            get
            {
                if (_headerCollection.AllKeys.Contains(key))
                {
                    return _headerCollection[key];
                }
                return null;
            }
            set { _headerCollection.Add(key,value);}
        }

        public Headers()
        {
            Accept = null;
            Connection = null;
            ContentLength = -1;
            ContentType = null;
            Date = DateTime.MinValue;
            Expect = null;
            Host = null;
            IfModifiedSince = DateTime.MinValue;
            KeepAlive = true;
            Expect100Continue = true;
            Referer = null;
        }

        private readonly WebHeaderCollection _headerCollection = new WebHeaderCollection();

        public WebHeaderCollection HeaderCollection => _headerCollection;
        public string Accept { get; set; }
        public string Connection { get; set; }
        public long ContentLength { get; set; }
        public string ContentType { get; set; }
        public DateTime Date { get; set; }
        public string Expect { get; set; }
        public string Host { get; set; }
        public DateTime IfModifiedSince { get; set; }
        public string Range { get; set; }
        public string Referer { get; set; }
        public string UserAgent { get; set; }
        public bool Expect100Continue { get; set; }
        public bool KeepAlive { get; set; }

        public string AcceptCharset
        {
            get { return this["Accept-Charset"]; }
            set { this["Accept-Charset"] = value; }
        }
        public string AcceptEncoding
        {
            get { return this["Accept-Encoding"]; }
            set { this["Accept-Encoding"] = value; }
        }
        public string AcceptLanguage
        {
            get { return this["Accept-Language"]; }
            set { this["Accept-Language"] = value; }
        }
        public string AcceptRanges
        {
            get { return this["Accept-Ranges"]; }
            set { this["Accept-Ranges"] = value; }
        }
        public string Authorization
        {
            get { return this["Authorization"]; }
            set { this["Authorization"] = value; }
        }
        public string CacheControl
        {
            get { return this["Cache-Control"]; }
            set { this["Cache-Control"] = value; }
        }
        public string Cookie
        {
            get { return this["Cookie"]; }
            set { this["Cookie"] = value; }
        }
        public string From
        {
            get { return this["From"]; }
            set { this["From"] = value; }
        }
        public string IfMatch
        {
            get { return this["If-Match"]; }
            set { this["If-Match"] = value; }
        }
        public string IfNoneMatch
        {
            get { return this["If-None-Match"]; }
            set { this["If-None-Match"] = value; }
        }
        public string IfRange
        {
            get { return this["If-Range"]; }
            set { this["If-Range"] = value; }
        }
        public string IfUnmodifiedSince
        {
            get { return this["If-Unmodified-Since"]; }
            set { this["If-Unmodified-Since"] = value; }
        }
        public string MaxForwards
        {
            get { return this["Max-Forwards"]; }
            set { this["Max-Forwards"] = value; }
        }
        public string Pragma
        {
            get { return this["Pragma"]; }
            set { this["Pragma"] = value; }
        }
        public string ProxyAuthorization
        {
            get { return this["Proxy-Authorization"]; }
            set { this["Proxy-Authorization"] = value; }
        }
        public string Te
        {
            get { return this["TE"]; }
            set { this["TE"] = value; }
        }
        public string Upgrade
        {
            get { return this["Upgrade"]; }
            set { this["Upgrade"] = value; }
        }
        public string Via
        {
            get { return this["Via"]; }
            set { this["Via"] = value; }
        }
        public string Warning
        {
            get { return this["Warning"]; }
            set { this["Warning"] = value; }
        }

        public IDictionary<string, string> CustomPairs
        {
            set
            {
                if (value != null)
                {
                    foreach (KeyValuePair<string, string> pair in value)
                    {
                        this.Add(pair.Key, pair.Value);
                    }
                }
            }
        }

        public void Add(string key, string value)
        {
            _headerCollection.Add(key,value);
        }

        public void Remove(string key)
        {
            _headerCollection.Remove(key);
        }
    }
}
